<?php

class mediaFile{
	function getUserFileList($user_id,$orderBy,$num=0, $isPage = false,$isMore = true){
		global $dbs,$user,$page,$encrypt;
		if( ''==$orderBy) $orderBy = 'id';
		
		if( $user -> id() == $user_id || $user -> powder() >= 9  )
			$sqlCount = 'SELECT * FROM `'. DB_PRE .'file` WHERE `user_id` = '. $user_id .' ORDER BY `'. $orderBy .'` DESC';
		else
			$sqlCount = 'SELECT * FROM `'. DB_PRE .'file` WHERE `user_id` = '. $user_id .' AND `status` = "1" ORDER BY `'. $orderBy .'` DESC';
			
		if( $num == 0 )
			$sql = $sqlCount .' limit '. (($page - 1) *  PAGE_SIZE ) .' , '. PAGE_SIZE;
		else
			$sql = $sqlCount .' limit 0 , '. $num;		
		$listCount	= $dbs -> select($sqlCount);
		$total			= count($listCount);
		
		$lists = $dbs -> select($sql);
		$html = '';
		
		$userInfo = $user -> getUser($user_id);
		
		foreach($lists as $list){
			$starHtml =  '';
			
			$starHtml = '';
			for($i = 1; $i <= 5 ; $i++){
				$class = $i <= $list['star'] ? ' class="a" ' : '';
				$starHtml .= '<i'.$class.'></i>';
			}
		
		
			$html .= '<li>'
			  .'<a href="'.urlRewrite('option=media&id='.$encrypt -> code($list['id'])).'" target="_blank"><img src="'.getMediaThumb($list['id']).'" /></a>'
			  .'<h2>'. htmlShow($list['name']).'</h2>'
			  .'<h3 class="star">'.lang_grade.'：'.$starHtml.'</h3>'
			  .'<h3>'.lang_posted_by.'：<a href="'.urlRewrite('option=home&id='.$list['user_id']) .'" target="_blank">'. htmlShow($userInfo['nickName']) .'</a></h3>'
			  .'<h1>'. getTag($list['id'],'_blank') .'</h1>'
			.'</li>';
		}
		$html .= '<div class="clear"></div>';
		 
		if($isPage)
			$html .= getPages($page,$total,getPageFileUrl() .'&order='. $orderBy ,PAGE_SIZE, PAGE_LEN);
		if($isMore && $total > $num)
			$html .= '<div class="more"><a href="'.urlRewrite('option=mylist&id='.$user_id .'&page=1.html'  ).'" target="_blank">'. lang_all .' &gt;</a></div>';
		return $html;
	}
	
	
	function getMyFileList( $user_id ,$num= '', $isPage = false,$isMore = true){
		global $dbs , $user , $page ,$encrypt ;
		$target = '_blank';
		$subStrLen = FILE_LIST_DESCRIPTION_LEN ? FILE_LIST_DESCRIPTION_LEN : 100 ;
		$html ='<table width="100%" border="0" cellspacing="1" cellpadding="3">'
				  .'<tr class="listTitle">'
					.'<th class="works">'.lang_works.'</th>'
					//.'<th class="name">名称</th>'
					//.'<th class="time">时间</th>'
					.'<th class="score">'.lang_grade.'</th>'
					//.'<th class="hits">点击</th>'
					//.'<th class="down">下载</th>'
					//.'<th class="comm">评论</th>'
					.'<th class="operate">'.lang_operate.'</th>'
				  .'</tr>';
		// ;
		//if( $user -> id() == $user_id || $user -> powder() >= 9  )
		if( $user -> id() == $user_id || $user -> powder() >= 9)
			$sqlCount = 'SELECT * FROM `'.DB_PRE.'file` WHERE `user_id` ='. $user_id .' ORDER BY `id` DESC' ;
		else
			$sqlCount = 'SELECT * FROM `'.DB_PRE.'file` WHERE `user_id` ='. $user_id .' AND `status` = "1" ORDER BY `id` DESC' ;

		if( $num == '' )
			$sql = $sqlCount .' limit '. (($page - 1) *  PAGE_SIZE ) .' , '. PAGE_SIZE;
		else
			$sql = $sqlCount .' limit 0 , '. $num;		
		$listCount	= $dbs -> select($sqlCount);
		$total			= count($listCount);
		
		$lists = $dbs -> select( $sql );
		
		foreach($lists as $list ){
			$html .= fileListHtml($list );
		}
		
		if( count( $lists ) == 0 )
			$html .= '<tr><td colspan="10" class="noWorks"><p class="emptylist">未上传任何作品，点击<a href="'. urlRewrite('option=creatMedia').'">这里</a>开始上传。</p></td></tr>';
		
		//if($isPage)
			$html .= '<tr><td colspan="10" >'.getPages($page,$total,getPageFileUrl('id='.$user_id,true) ,PAGE_SIZE  ,PAGE_SIZE) .'</td></tr>';
		
		$html .= '</table>';
		echo $html;
	}	
	
	function del($file_id){
		global $dbs,$user,$dataType;		
		
		self::checkStatus( $file_id );
		
		$lists = $dbs -> select('SELECT * FROM `'.DB_PRE.'file` WHERE `id` = '. $file_id);
		$lists = $lists[0];
		if( count($lists) == 0 )
			exit('{"status":"error","msg":"错误的参数，作品不存在."}');	
			
		
		if( $lists['lock'] == '1' && $user -> powder() < 9 ){
			exit('{"status":"error","msg":"作品被锁定,不能删除."}');	
		}
		 
		// 或者是作者并且作品不能被锁定,  或者是管理员
		if( ($lists['user_id'] == $user -> id() ) || $user -> powder() >= 9 ){			
			$files 			 = $lists['file'];
			$thumbs 		 = $lists['thumb'];
			$mediaSources	 = $lists['mediaSource'];
			
			self::delFile($files);
			self::delFile($thumbs);
			self::delFile($mediaSources);
			
			$dbs -> delete( DB_PRE .'file' , ' `id` = '. $file_id );
			$dbs -> delete( DB_PRE .'filetag' , ' `parentId` = '. $file_id );			
			$dbs -> delete( DB_PRE .'comments' , ' `parent_id` = '. $file_id );
			
			$dbs -> delete( DB_PRE .'task_attend' , ' `file_id` = '. $file_id );

			if( $dataType == 'ajax')
				exit('{"status":"ok","msg":"删除成功"}');	
			else
				//infoPage('删除成功', urlRewrite('option=mylist&id='.$user->id()) .'&page=1.html',2);
				infoPage('删除成功','',2);
				exit();
				
		}else{
			exit('{"status":"error","msg":"你没有权限删除."}');
		}
	}
	
	function delFile($files){
		$files = explode('|',$files);
		$allowFolder = str_replace( DS , '/' , USER_CAN_WRITE_FOLDER );
		$allowFolder = explode('|',$allowFolder) ;
		foreach($files as $file){
			$filePath = explode('/',$file);
			//echo(ROOTPATH . $file.'----'.file_exists( ROOTPATH . $file ).'---'.$filePath[count($filePath)-1] .'============'. $filePath[0].'/' . '==========='. $allowFolder  . '========>>>'.in_array($filePath[0].'/',$allowFolder) ) ;
			if( file_exists( ROOTPATH . $file) && $filePath[count($filePath)-1] != '' && in_array($filePath[0].'/',$allowFolder)  ){
				unlink( ROOTPATH . $file);
			}
		}
		
	}
	
	function isFileCanDel($file_id){
		global $dbs,$user;
		$show = $dbs -> select('SELECT * FROM `'.DB_PRE.'file` WHERE `id` = '. (int)$file_id);
		$show = $show[0];
		$isCan = false ;
		if( ($show['user_id'] == $user -> id() || $user -> powder() >= 9 ) && $show['lock'] == '0' ){
			$isCan = true;
		}
		
		return $isCan ;
	}
	
	function isFileCanEdit($file_id){
		global $dbs,$user;
		$show = $dbs -> select('SELECT * FROM `'.DB_PRE.'file` WHERE `id` = '. (int)$file_id);
		$show = $show[0];
		$isCan = false ;
		if( ($show['user_id'] == $user -> id() || $user -> powder() >= 9 ) && $show['lock'] == '0' ){
			$isCan = true;
		}
		return $isCan ;
	}
	
	function showStatus($file_id){
		global $dbs,$user,$encrypt;
		$show = $dbs -> select('SELECT * FROM `'.DB_PRE.'file` WHERE `id` = '. (int)$file_id);
		$show = $show[0];
		$html = '' ;
		$idCode = $encrypt -> code($file_id);
		if( ( $show['user_id'] == $user -> id() || $user -> powder() >= 9 ) && ($show['status'] == '0' || $show['status'] == '-1' ) ){
			$html ='<a id="statusPublic" class="actionstatus statusPublic" href="'.WEB_ROOT_PATH.'?option=userDo&type=action&do=changeFileStatus&status=1&id='.$idCode.'">'.lang_is_hide.'</a>';
		}else if( ( $show['user_id'] == $user -> id() || $user -> powder() >= 9 ) && $show['status'] == '1' ){
        	$html ='<a id="statusPrivate" class="actionstatus  statusPrivate" href="'.WEB_ROOT_PATH.'?option=userDo&type=action&do=changeFileStatus&status=0&id='.$idCode.'">'.lang_is_show.'</a>';
        }else{
            $html ='<a class="disabled">'.lang_set_show.'</a>';
        }
		return $html;
        
        
	}
	
	
	//status ={ -1:locked ,  0:draft  , 1:normal };
	function changeStatus($file_id = 0,$status = '1'){
		global $dbs,$user,$dataType;
		
		$lists = $dbs -> select('SELECT * FROM `'.DB_PRE.'file` WHERE `id` = '. $file_id);
		$lists = $lists[0];
		
		 
		// 或者是作者并且作品不能被锁定,  或者是管理员
		if( ($lists['user_id'] == $user -> id() && $lists['status'] != '-1' ) || $user -> powder() >= 9 ){	
			$array['status'] = (int)$status ;
			$dbs -> update( DB_PRE .'file',$array,'`id` = '. $file_id);
			//exit('{"status":"ok","msg":"修改成功."}');
			exit( infoPage('','',0.001) );
		}else{
			exit('{"status":"error","msg":"您没有权限更改."}');
			exit( infoPage('','',0.001) );	
		}		
		
	}
	
	
	function checkStatus( $file_id = 0 ){
		global $dbs ;
		$file_id = (int)$file_id;
		$file = $dbs -> select('SELECT * FROM `'.DB_PRE.'file` WHERE `id` = '. $file_id .' limit 0,1');
		$file = $file[0];
		
		if( '1' == $list['lock'] || count($file) == 0 )
			return ;
		
		$isLock = false;
		$taskLists = $dbs -> select('SELECT * FROM `'.DB_PRE.'task_attend` WHERE `file_id` = '. $file['id'] );
		foreach($taskLists as $tasks ){
			$task = $dbs -> select('SELECT * FROM `'.DB_PRE.'task` WHERE `id` = '. $tasks['task_id'] );
			$task = $task[0];
			if( (float)date('Ymd') > (float)str_replace('-','',$task['timeEnd'])  ){
				$isLock = true ;
				break ;
			}				
		}
		
		if($isLock){
			// 锁定作品
			$array = array();
			$array['lock'] = '1';
			//$update['lock'] = '1';
			$dbs -> update( DB_PRE .'file', $array , ' `id` = '. $file['id'] );		
		}
	}
	
	
	function is(){
	
	}
	
	
	
}
?>